package com.sf.osms.dep.processor.util;

import java.util.Date;

import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.domain.RouterProblemRecord;
import com.sf.osms.dep.processor.exception.RouterException;
import com.sf.osms.dep.processor.model.EventModel;

public class ExceptionUtils {

	public static String getExceptionStackString(Exception e) {
	    StackTraceElement[] stes = e.getStackTrace();
	    StringBuilder errorMsg = new StringBuilder(e.toString());
	    if(stes!=null && stes.length>0){
	    	//记录出错时的栈信息
		    for(StackTraceElement ste:stes){
		    	errorMsg.append(ste.toString()).append("\n");					    	
		    }
	    }
	    return errorMsg.toString();
	}
	
	public static void buildRouterProblemRecord(EventModel eventModel,Agent agentInfo,RouterException e){
		RouterProblemRecord rpr = new RouterProblemRecord();
		rpr.setAgentName(agentInfo.getName());
		rpr.setCreateTime(new Date());
		rpr.setFileName(eventModel.getFile().getName());
		rpr.setLocalFilePath(agentInfo.getBackPath() + "/" + eventModel.getFile().getName());
		rpr.setErrorCode(e.getExceptionCode());
		rpr.setErrorDesc(e.getMsg());
		rpr.setState(0);
		eventModel.getErrorEventRecord().add(rpr);
		
	}
}
